<?php
namespace BackHome\Controller;
use Think\Controller;
class IndexController extends Controller {
	//前置操作方法
    public function _before_index(){
		//检查是否登录
		if(session('id')>0){
			
			}else{
				redirect('/BackHome/Index/login', 1, '请您先登录...');
			}
    }
	
	//后台首页
    public function index(){
		$this->display();
    }//end fun
	
	//加载默认首页
	public function loadindex(){
		$this->display();
	}//end fun
	
	//登出
	public function logout(){
		session('id',null); // 删除id
		redirect('/BackHome/Index/login', 1, '登录已退出...');
	}
	
	//登录
	public function login(){
		$admin=D('admin');
		if(empty($_POST["name"])){
			//检查是否登录
			if(session('id')>0){
				$this->success('不用重复登录！', '/BackHome/Index/');
				}else{
					$this->display();
				}
			}else{
				//登录
				//dump($_POST);
				$getone=$admin->where("uname='".$_POST["name"]."' and upwd='".md5($_POST["pwd"])."'")->find();//dump($getone);
				if(is_numeric($getone['id'])){
					session('id',$getone['id']);  //设置session
					$admin-> where('id='.$getone['id'])->setField('logintime',time());
					$this->success('您已成功登录！', '/BackHome/Index/');
					}else{
						$this->error('登录失败','/BackHome/Index/login',5);
					}
				
			}
		
	}//end fun
	
}